const express = require('express')
const rootUserModel = require('../Model/rootUserModel')
const router = express.Router()
let i = 0
//初始化管理员
router.post('/RootUser', async (req, res, next) => {
    await rootUserModel.create({ user:'admincbb', password:'admincbb' })
})
//获取管理员数据
router.post('/getRootUser', async (req, res, next) => {
    const dataSum = await rootUserModel.find()
    const data = await rootUserModel.find().limit(10)
    res.json({ code: '200', msg: dataSum.length, data })
})
//添加管理员数据接口
router.post('/addRootUser', async (req, res, next) => {
    const { user, password, thisPage } = req.body
    if (thisPage !== 0) i = (thisPage - 1) * 10
    if (user !== '' && password !== '') {
        await rootUserModel.create({ user, password })
        const data = await rootUserModel.find().skip(i).limit(10)
        const dataSum = await rootUserModel.find()
        res.json({ code: '200', msg: dataSum.length, data })
        return
    }
    res.json({ code: '404' })
})
//管理员分页
router.post('/getpageData', async (req, res, next) => {
    const { thisPage } = req.body
    if (thisPage !== 0) i = (thisPage - 1) * 10
    const data = await rootUserModel.find().skip(i).limit(10)
    res.json({ code: '200', data })
})
//删除管理员账号
router.post('/deleteRootUser', async (req, res, next) => {
    const { _id,thisPage } = req.body
    if (thisPage !== 0) i = (thisPage - 1) * 10
    await rootUserModel.deleteOne({_id})
    const dataSum = await rootUserModel.find()
    const data = await rootUserModel.find().skip(i).limit(10)
    res.json({ code: '200', msg: dataSum.length, data })
})

module.exports = router